package com.zhushenwudi.hmutils.net;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

/**
 * 网络请求构建器基类
 */
public abstract class BaseNetworkApi {
    /**
     * 配置HTTP
     */
    private final OkHttpClient okHttpClient = setHttpClientBuilder(new OkHttpClient.Builder()).build();

    public <T> T getApi(Class<T> serviceClass, String baseUrl) {
        Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(okHttpClient);
        return setRetrofitBuilder(retrofitBuilder).build().create(serviceClass);
    }

    /**
     * 实现重写父类的setHttpClientBuilder方法，
     * 在这里可以添加拦截器，可以对 OkHttpClient.Builder 做任意操作
     */
    public abstract OkHttpClient.Builder setHttpClientBuilder(OkHttpClient.Builder builder);

    /**
     * 实现重写父类的setRetrofitBuilder方法，
     * 在这里可以对Retrofit.Builder做任意操作，比如添加GSON解析器，Protocol
     */
    public abstract Retrofit.Builder setRetrofitBuilder(Retrofit.Builder builder);
}
